/***************************************************************************
 *   Copyright (C) 2008 by Łukasz Spintzyk   *
 *   lukasz.spintzyk@gmail.com   *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/
#ifndef AD_CONVERSION_H
#define AD_CONVERSION_H
#include <avr/io.h>
#include <inttypes.h>
#include <avr/interrupt.h>
#include <inttypes.h>
#include "device_opt.h"
#include "digital_oscilloscope.h"
#include "display.h"
/*!------------------------------------------------------------------
 * ---ANALOG TO DIGITAL CONVERTER------------------------------------
 * ------------------------------------------------------------------
 * 
 * Definition of constants, variables, and functions which serve builit-n ATMEGA and external ADC
 */
void adc_init();
uint8_t getExtADCValue();
extern uint8_t ADC_conversion;//! State of ADC conversion

#define ADC_DONE 1
//! Read of channel2  value  from AVR_ADC is finished
#define ADC_AVR 2
//! Read of analog-key value is finished
#define ADC_AKEY 0
//! Read of channel2  value  from external ADC is finished
#define ADC_EXT 3

extern int8_t adc_key; //!< Byte holds last value measured on potentiometr.
extern int8_t adc_avr; //!< Byte holds last value measured on avr_adc
extern int8_t adc_ext; //!< Byte holds last value measured on external ADC




//Choose voltage reference and analog input pin.
//--------------------------------------------------------------------ADMUX------------------------
inline void SET_VOLTAGE_REF(uint8_t x);	//x=0 - 3
#define AREF_V 0
#define AVCC_V 1
#define INTERNAL1_1 2
#define INTERNAL2_56 3
#define GET_VOLTAGE_REF() (((ADMUX)&(0xC0))>>6)


#define SET_LEFT_ADJUST() ADMUX|=1<<ADLAR
#define SET_RIGHT_ADJUST() ADMUX&=~(1<<ADLAR)
#define GET_ADJUST() (ADMUX&(1<<ADLAR))


inline void SET_ANALOG_CH_AND_GAIN(uint8_t x);
inline uint8_t GET_ANALOG_CH_AND_GAIN();
//for Single Ended input ADMUX bits  4..0 are te same as phisical pin numbers of ADC port



//Control And state register A of ADC
//---------------------------------------------------------------------------ADCSRA
#define START_ADC() ADCSRA|=1<<ADSC
#define ENABLE_ADC() ADCSRA|=1<<ADEN
#define DISABLE_ADC() ADCSRA&=~(1<<ADEN)
#define ENABLE_ADC_TRIGGER() ADCSRA|=1<<ADATE
#define DISABLE_ADC_TRIGGER() ADCSRA&=~(1<<ADATE)
#define ENABLE_ADC_INTERUPT() ADCSRA|=1<<ADIE
#define DISABLE_ADC_INTERUPT() ADCSRA&=~(1<<ADIE)
inline void SET_ADC_PRESCALER(uint8_t x);
#define PRESCALER2 1
#define PRESCALER4 2
#define PRESCALER8 3
#define PRESCALER16 4
#define PRESCALER32 5
#define PRESCALER64 6
#define PRESCALER128 7

//Control And state register B of ADC

//------------------------------------------------------------------------------------------ADCSRB
inline void SET_AUTO_TRIGGER_SRC(uint8_t x);
#define FREE_RUNNING 0


//Register availible in atmega64
//------------------------------------------------------------------------------------------DIDR0
//#define SET_DIDR0(x) DIDR0=x
//#define DIDR0_POWER_SAVE0 SET_DIDR0(0xFF)
//#define DIDR0_INPUT0 SET_DIDR0(0)

//Register availible in atmega64
//------------------------------------------------------------------------------------------DIDR1
//#define SET_DIDR1(x) DIDR1=x
//#define DIDR0_POWER_SAVE1 SET_DIDR1(0xFF)
//#define DIDR0_INPUT1 SET_DIDR1(0)

//retrive value from Atmega internal adc converter
inline int8_t getAVRADC();

//turn on reading value FROM ANALOG KEY
void runADC_AKey(unsigned char on);

//-------------------------------------------------------------------------------------


#endif
